package stuman.activity;

import stuman.entity.Grade;
import stuman.entity.Section;
import stuman.entity.Student;
import stuman.entity.Subject;
import stuman.util.Container;
import stuman.util.SaveLoad;

import com.StuMan.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class SectionView extends Activity{
	
	private Student student;
	private Subject subject;
	private Section section;
	
	@Override
    public void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.section_view);
		
		Bundle bun = getIntent().getExtras();
		
		student = Container.getStudent(bun.getString("student"));
		subject = student.getSubject(bun.getString("subject"));
		section = subject.getSection(bun.getString("section"));
		
		loadSectionInformation();
		
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.section_menu, menu);
        return true;
    }
	
	public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection

        switch (item.getItemId()) {
        	case R.id.createGrade:
        		//Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();
        		AlertDialog.Builder builder = new AlertDialog.Builder(SectionView.this);
        		LinearLayout ll = new LinearLayout(this);
        		final EditText et = new EditText(this);
        		et.setWidth(100);
        		ll.addView(et);
        		
        		builder.setView(ll);
        		builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
        	           public void onClick(DialogInterface dialog, int id) {
        	               Grade g = new Grade(et.getText().toString());
        	               section.addGrade(g);
        	               Intent intent = new Intent(SectionView.this, SectionView.class);
        	   			   intent.putExtra("section", section.getName());
        	   		  	   intent.putExtra("subject", subject.getName());
        	   			   intent.putExtra("student", student.getFirstName());
        	   			   startActivity(intent);
        	           }
        	       });
        		AlertDialog alert = builder.create();
        		alert.show();
        		return true;
        	default:
                return super.onOptionsItemSelected(item);
       }
	}
	
	private void loadSectionInformation(){
		TextView sectionName = (TextView) findViewById(R.id.lblSectionName);
		sectionName.setText(section.getName().toString());
		
		TableLayout tl = (TableLayout) findViewById(R.id.sectionViewTableLayout);
		tl.removeAllViews();
		for (int i=0;i<section.getGrades().size();i++){
			TableRow tr = new TableRow(this);
			final TextView tv = new TextView(this);
			tv.setTextSize(20);
			tv.setText(section.getGrades().get(i).getName());
			
			
			tr.addView(tv);
			tl.addView(tr);
		}
	}

}
